//
//  BinSHopViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/16.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BinSHopViewController.h"
#import "WqGaoDeLocationViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "BaseNavViewController.h"
#import "TabBarViewController.h"
#import "BshopZYCPViewController.h"

@interface BinSHopViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *tishiUplogoBut;
@property (weak, nonatomic) IBOutlet UIButton *UplogoBut;
@property (weak, nonatomic) IBOutlet UITextField *shopnameTF;
@property (weak, nonatomic) IBOutlet UITextField *lianxirenTF;
@property (weak, nonatomic) IBOutlet UITextField *lianxidianhuaTF;
@property (weak, nonatomic) IBOutlet UITextField *xiangxidizhiTF;
@property (weak, nonatomic) IBOutlet UIButton *zhuyingBut;

@property(nonatomic,strong)NSMutableDictionary *mydic;
@property (weak, nonatomic) IBOutlet UIImageView *shopImage;

@property(nonatomic,strong)UIImage *mylogoimage;
@property(nonatomic,assign)BOOL upimage;

@property (weak, nonatomic) IBOutlet UIButton *seleAddressBut;
@property(nonatomic,strong)NSString *zhuyingyew;

@end

@implementation BinSHopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"电器店信息";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];

    self.mydic = [NSMutableDictionary dictionaryWithCapacity:1];
    self.mylogoimage = [[UIImage alloc]init];
    
    self.upimage = YES;
    
    [self.rightbutton setTitleColor:zhutiColor forState:(UIControlStateNormal)];
    [self.rightbutton setTitle:@"确定" forState:(UIControlStateNormal)];
    // Do any additional setup after loading the view from its nib.
}
- (void)rightClick{
    //完成
    
    if (self.upimage) {
        ShowToastWithText(@"请上传店铺图片");
        return;
    }
    if (strIsEmpty(self.shopnameTF.text)) {
        ShowToastWithText(@"请输入店铺名称");
        return;
        
    }
    
    if (strIsEmpty(self.lianxirenTF.text)) {
        ShowToastWithText(@"请输入联系人");
        return;
        
    }
    if (strIsEmpty(self.lianxidianhuaTF.text)) {
        ShowToastWithText(@"请输入联系电话");
        return;
        
    }
    if (![HFTools isMobileNumberOnly:_lianxidianhuaTF.text]) {
        ShowToastWithText(@"请输入正确的电话号码");
        return;
        
    }
    
    if ([_mydic allKeys].count <2) {
        ShowToastWithText(@"请选择地址");
        return;
        
        
    }
    if (strIsEmpty(self.xiangxidizhiTF.text)) {
        ShowToastWithText(@"请输入详细地址");
        return;
        
    }
    if (strIsEmpty(_zhuyingyew)) {
        ShowToastWithText(@"请选择主营业务");
        return;
        
    }
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //param[@"type"] = @"logo";
    NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithCapacity:1];
    mudic[@"agent_id"]=  KUID;
    mudic[@"name"]=  _shopnameTF.text;
    mudic[@"telephone"]=  _lianxidianhuaTF.text;
    mudic[@"lon"]=  [_mydic objectForKey:@"longitude"];
    mudic[@"lat"]=   [_mydic objectForKey:@"latitude"];
    mudic[@"address"]=   _xiangxidizhiTF.text;
    mudic[@"area"]=  [_mydic objectForKey:@"area"];
    mudic[@"product"]=   _zhuyingyew;
    mudic[@"person"]=   _lianxirenTF.text;

    
    NSString *jsion = [HFTools toJSONString:mudic];
    
    param[@"data"] = jsion;
    kWeakSelf;
    [NetWorkTool UploadPicWithUrl:addNearShop param:param key:@"image" image:_mylogoimage withSuccess:^(id dic) {
        
        [MBProgressHUD hideHUDForView:    weakSelf.view animated:YES];
        ShowToastWithText(@"添加成功成功");
        if (weakSelf.myblock) {
            weakSelf.myblock(@"");
        }
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        
    }];
}
- (IBAction)uplogoAction:(id)sender {
    [self uplogo];

}
- (IBAction)tishiuplogoaction:(id)sender {
    [self uplogo];

}

- (IBAction)seleAddress:(UIButton *)sender {
    //选择地址
    [self seleaddress];

}
- (IBAction)seleactionsdfs:(UIButton *)sender {
    [self seleaddress];

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)seleaddress{
    //选择地址
    if ([self isLocationServiceOpen]) {
        
        
        WqGaoDeLocationViewController *locaVC = [[WqGaoDeLocationViewController alloc]init];
        BaseNavViewController * navigation = [[BaseNavViewController alloc]initWithRootViewController:locaVC];
        //        [self.logoBUt setTitle:[locaVC.locadic objectForKey:@"building_name"] forState:UIControlStateNormal];
        locaVC.locaBlock = ^(NSMutableDictionary *locadic) {
          
            NSString *ssqj = [NSString stringWithFormat:@"%@ %@ %@",[locadic objectForKey:@"province"],[locadic objectForKey:@"city"],[locadic objectForKey:@"district"],[locadic objectForKey:@"building_name"]];
            
            if ([[locadic allKeys] containsObject:@"locadic"]) {
                NSString *afff = [NSString stringWithFormat:@"%@",[locadic objectForKey:@"address"]];
                [self.seleAddressBut setTitle:afff forState:UIControlStateNormal];

            }else{
                [self.seleAddressBut setTitle:ssqj forState:UIControlStateNormal];

            }
            
            
            [self.seleAddressBut setTitleColor:K333333 forState:UIControlStateNormal];
            self.seleAddressBut.titleLabel.numberOfLines = 0;
            //            [self.locadic removeAllObjects];
            self.mydic = locadic;
            KMyLog(@"看看都有啥%@",locadic);
        };
        [self presentViewController:navigation animated:YES completion:^{
            //        [self.locationButton setTitle:[locaVC.locadic objectForKey:@"building_name"] forState:UIControlStateNormal];
            //        NSLog(@"确定kkkkkkkkkk00%@",locaVC.locadic);
            //
            //        self.locadic = locaVC.locadic;
        }];
    }else{
        //未开启定位权限
        UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"需要您开启定位权限,是否允许" preferredStyle:UIAlertControllerStyleAlert];
        
        [alertCon addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            KMyLog(@"取消");
        }]];
        [alertCon addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            KMyLog(@"确定");
            if ([[[UIDevice currentDevice] systemVersion] integerValue] >9.9) {
                
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                
            }else
                
            {
                [[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                
                //
                //                NSURL *url = [NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"];
                //
                //                [[UIApplication sharedApplication] openURL:url];
                
            }
            
            
            
            
            
            
        }]];
        
        
    }
    
    
}
-(void)uplogo{
    //选择logo照片
    UIImagePickerController *pick = [[UIImagePickerController alloc]init];
    pick.allowsEditing = YES;
    pick.delegate = self;
    [UIAlertController showPhotoAlerControllerPicker:pick controller:self sourceView:nil];
    
}
- (void)upLoadHeadIcon:(UIImage *)image {
    self.mylogoimage = image;
    self.upimage = NO;
    self.tishiUplogoBut.hidden = YES;
    self.shopImage.image = image;
    //    [MBProgressHUD showHUDAddedTo:self.view msg:@"上传头像..." animated:YES];
    //
    //    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //    //param[@"type"] = @"logo";
    //
    //    [NetWorkTool UploadPicWithUrl:changeInfo param:param key:@"images" image:image withSuccess:^(id dic) {
    //        [MBProgressHUD hideHUDForView:self.view animated:YES];
    //        self.upimage = NO;
    //    } failure:^(NSError *error) {
    //        [MBProgressHUD hideHUDForView:self.view animated:YES];
    //    }];
    
}



- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImage *image = info[UIImagePickerControllerEditedImage];
    
    
    [self upLoadHeadIcon:image];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
//是否开启定位
- (BOOL)isLocationServiceOpen {
    if ([ CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
        return NO;
    } else
        return YES;
}
- (IBAction)sele:(UIButton *)sender {
    sender.titleLabel.numberOfLines = 0;
    [self swlwzhuyaningyeu];
}


- (IBAction)seleimage:(UIButton *)sender {
    [self swlwzhuyaningyeu];
}

-(void)swlwzhuyaningyeu{
    
    BshopZYCPViewController *zhuying = [[BshopZYCPViewController alloc]init];
    kWeakSelf;
    zhuying.myblock = ^(NSString * _Nonnull name, NSString * _Nonnull idstr) {
        [weakSelf.zhuyingBut setTitle:name forState:(UIControlStateNormal)];
        weakSelf.zhuyingyew =idstr;
    };
    
    [self.navigationController pushViewController:zhuying animated:YES];
}
@end

